﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LGE;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;

namespace UpUp
{
    public class Geiser : GameObject
    {
        double timer = 0;
        double timeToChangeState = 0;
        SpriteSheetState state;

        public Geiser(PlayScreen ps, Vector2 position)
            : base(ps, Engine.Load<Texture2D>("Images\\Geiser\\active"))
        {
            state = new SpriteSheetState(this);
            state.Add(Engine.Load<Texture2D>("Images\\Geiser\\start"), new SpriteSheet(this, 3, 50, false));
            state.Add(Engine.Load<Texture2D>("Images\\Geiser\\active"), new SpriteSheet(this, 8, 50, true));
            state.Add(Engine.Load<Texture2D>("Images\\Geiser\\end"), new SpriteSheet(this, 3, 50, false));
            state.Add(Engine.Load<Texture2D>("Images\\Geiser\\end"), new SpriteSheet(this, 3, 100, true)); // off
            state.Change(0);
            sourceRectangle = new Rectangle(0, 0, 128, sprite.Height);
            this.position = position - new Vector2(0, Tile.Size - 5);
            timeToChangeState = getRandTime();
        }

        public override void Update(GameTime gameTime)
        {
            if (!alive) return;

            if (state.actualState == 1 && Collision(ps.c)) 
            {
                ps.c.hp = 0;
            }

            timer += gameTime.ElapsedGameTime.TotalMilliseconds;
            if (timer > timeToChangeState)
            {
                timer = 0;
                state.ChangeToNext();
                timeToChangeState = getRandTime();
                state.spriteSheets[state.actualState].actualFrame = 0;
                state.spriteSheets[state.actualState].intervalAcc = 0;
            }
            else if (state.completed && (state.actualState == 0 || state.actualState == 2))
            {
                timer = 0;
                state.ChangeToNext();
                timeToChangeState = getRandTime();
                state.spriteSheets[state.actualState].actualFrame = 0;
                state.spriteSheets[state.actualState].intervalAcc = 0;
            }
            
            state.Update(gameTime);
        }

        public override void Draw(SpriteBatch spriteBatch)
        {
            if (state.actualState != 3) base.Draw(spriteBatch);
        }

        private float getRandTime()
        {
            switch (state.actualState)
            {
                case 0: return 120;
                case 1: return Rand.NextFloat(500, 2000);
                case 2: return 120;
                default: return Rand.NextFloat(1500, 5000);
            }
        }

        public void Die()
        {
            alive = false;
        }
    }
}
